package pl.edu.agh.intobl.operators;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.uncommons.maths.random.Probability;
import org.uncommons.watchmaker.framework.operators.AbstractCrossover;

import pl.edu.agh.intobl.Route;
import pl.edu.agh.intobl.RouteSet;
import pl.edu.agh.intobl.exceptions.IntOblException;

public class RouteSetCrossOver extends AbstractCrossover<RouteSet> {

	public RouteSetCrossOver(int crossoverPoints, Probability crossoverProbability) {
		super(crossoverPoints, crossoverProbability);
	}

	@Override
	protected List<RouteSet> mate(RouteSet parent1, RouteSet parent2, int numberOfCrossoverPoints, Random rand) {
		List<RouteSet> twoCrossedChildren = new LinkedList<RouteSet>();
		
		try {
			Route subRoute = parent2.selectSubRoute(rand);
			int demanderNr = subRoute.findClosestDemandNotBelongingToRoute();
			RouteSet childRouteSet = parent1.diff(subRoute);
			childRouteSet.insertAfter(demanderNr, subRoute);
			childRouteSet.validateRoutes();
			twoCrossedChildren.add(childRouteSet);
			
			subRoute = parent1.selectSubRoute(rand);
			demanderNr = subRoute.findClosestDemandNotBelongingToRoute();
			childRouteSet = parent2.diff(subRoute);
			childRouteSet.insertAfter(demanderNr, subRoute);
			childRouteSet.validateRoutes();
			twoCrossedChildren.add(childRouteSet);
				
			return twoCrossedChildren;
		} catch (IntOblException e) {
			System.out.println("Jest nie dobrze");
			e.printStackTrace();
		}
		
		twoCrossedChildren.add(parent1);
		twoCrossedChildren.add(parent2);
		return twoCrossedChildren;
	}
}